
package com.eltonkent.ui.tasks;

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import android.content.Context;

import com.eltonkent.db.FolderListTable;
import com.eltonkent.db.SafeEntryTable;
import com.eltonkent.ui.ApplicationConstants;
import com.eltonkent.ui.ProgressListener;

public class RestoreTask extends Thread
{

	Context context;
	ProgressListener listener;

	public RestoreTask(Context context, ProgressListener listener)
	{
		this.context = context;
		this.listener = listener;
	}

	public void run()
	{

		try
		{
			FileInputStream fis = new FileInputStream(ApplicationConstants.BACK_UP_PATH
					+ ApplicationConstants.BACK_UP_FILENAME);
			listener.setProgressStatus(10);
			DataInputStream dis = new DataInputStream(fis);
			int pwd = dis.readInt();
			byte[] paswd = new byte[pwd];
			dis.read(paswd);
			/* discard pwd */
			paswd = null;
			FolderListTable folderTable = new FolderListTable(context);
			folderTable.deSerialize(dis);
			listener.setProgressStatus(35);
			SafeEntryTable entryTable = new SafeEntryTable(context);
			entryTable.deSerialize(dis);
			listener.setProgressStatus(90);
			dis.close();
			fis.close();
		} catch(FileNotFoundException e)
		{
			e.printStackTrace();
		} catch(IOException e)
		{
			e.printStackTrace();
		}

		listener.operationCompleted(true);

	}
}
